﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Web.Infrastructure.Mvc.Query.Attributes;

namespace Web.Infrastructure.Mvc.Query
{
    public class QueryModel : IValidatableObject
    {
        private int _maxPageSize;

        public QueryModel()
        {
            PageIndex = 1;
            PageSize = 10;
            _maxPageSize = 100;
        }

        [PageIndex]
        public int PageIndex { get; set; }

        [PageSize]
        public int PageSize { get; set; }

        [OrderBy]
        public string OrderBy { get; set; }

        [TotalCount]
        public int TotalCount { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (PageSize > _maxPageSize)
            {
                yield return new ValidationResult(string.Format("PageSize参数不可超过{0}", _maxPageSize));
            }
        }
    }
}